﻿using System;
using System.Text;
using System.Collections.Generic;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Scribd.Net;

namespace Scribd.Net.Tests
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class ServiceTests
    {
        public ServiceTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
        
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            Service.APIKey = Properties.Settings.Default.APIKey;
            Service.SecretKey = Properties.Settings.Default.SecretKey;
            Service.PublisherID = Properties.Settings.Default.PublisherId;
            Service.EnforceSigning = true;
            Service.WebProxy = null;

            Service.BeforePost += new EventHandler<ServicePostEventArgs>(Service_BeforePost);
            Service.AfterPost += new EventHandler<ServicePostEventArgs>(Service_AfterPost);
            Service.Error += new EventHandler<ScribdEventArgs>(Service_Error);
        }

        static void Service_Error(object sender, ScribdEventArgs e)
        {
            throw new AssertFailedException(e.Message);
        }

        static void Service_AfterPost(object sender, ServicePostEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(string.Format("ResponseXML: {0}", e.ResponseXML));
        }

        static void Service_BeforePost(object sender, ServicePostEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(string.Format("RESTCall: {0}", e.RESTCall));
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        /// <summary>
        /// Tests the Login functionality
        /// </summary>
        [TestMethod]
        public void LoginTest()
        {
            this.TestContext.BeginTimer("LoginTimer");
            User.LoggedIn += new EventHandler<UserEventArgs>(User_LoggedIn);
            User.Login(Properties.Settings.Default.UserName, Properties.Settings.Default.Password);
            User.LoggedIn -= new EventHandler<UserEventArgs>(User_LoggedIn);
            this.TestContext.EndTimer("LoginTimer");
        }

        void User_LoggedIn(object sender, UserEventArgs e)
        {
            Assert.IsTrue((sender as User).IsLoggedIn, "User has logged in.");
        }

        /// <summary>
        /// Tests the SignUp functionality.
        /// </summary>
        [TestMethod]
        public void SignupTest()
        {
            this.TestContext.BeginTimer("SignupTimer");
            string _userName = Properties.Settings.Default.SignUpUserName;
            string _password = Properties.Settings.Default.SignUpPassword;
            string _email = Properties.Settings.Default.SignUpEmail;
            string _name = Properties.Settings.Default.SignUpName;
            User.Signup(_userName, _password, _email, _name);
            Assert.IsTrue(Service.User.UserName == _userName);
            // Now try and log in as the test user.
            this.TestContext.BeginTimer("LoginTimer");
            User.LoggedIn += new EventHandler<UserEventArgs>(User_LoggedIn);
            User.Login(_userName, _password);
            User.LoggedIn -= new EventHandler<UserEventArgs>(User_LoggedIn);
            this.TestContext.EndTimer("LoginTimer");
            this.TestContext.EndTimer("SignupTimer");
        }

        [TestMethod]
        public void SlurpifyTest()
        {
            string _result = Service.Slurpify("http://www.codeplex.com/scribdnet", DisplayMode.Fullscreen, false);
            string _correct = string.Format(@"http://www.scribd.com/slurp?url=http://www.codeplex.com/scribdnet&display_mode=fullscreen&privacy=public&publisher_id={0}", Properties.Settings.Default.PublisherId);
            Assert.IsTrue((string.Compare(_correct, _result) == 0),"Slurped correctly!");
        }

        [TestMethod]
        public void UploadPublicAccessTest()
        {
            this.TestContext.BeginTimer("UploadTimer");
            Document.Uploaded += new EventHandler<DocumentEventArgs>(Document_Uploaded);
            Document _result = Document.Upload(Properties.Settings.Default.TestFilePath, "txt", false);
            Document.Uploaded -= new EventHandler<DocumentEventArgs>(Document_Uploaded);
            this.TestContext.EndTimer("UploadTimer");
        }

        [TestMethod]
        public void UploadPrivateAccessTest()
        {
            this.TestContext.BeginTimer("UploadTimer");
            Document.Uploaded += new EventHandler<DocumentEventArgs>(Document_Uploaded);
            Document _result = Document.Upload(Properties.Settings.Default.TestFilePath, AccessTypes.Private, 0, "txt", false);
            Document.Uploaded -= new EventHandler<DocumentEventArgs>(Document_Uploaded);
            this.TestContext.EndTimer("UploadTimer");
        }

        void Document_Uploaded(object sender, DocumentEventArgs e)
        {
            Assert.IsTrue(e.Document.DocumentId > 0, "Document uploaded.");
        }
    }
}
